// 定义本地存储的操作函数
function setItem(name, item) {
  localStorage.setItem(name, JSON.stringify(item));
}
function getItem(name) {
  return JSON.parse(localStorage.getItem(name) || '[]');
}

// 定义 Vuex 模块的状态
const state = {
  // 从本地存储中获取购物车商品信息
  car: getItem('car'),
   // 当前要购买的商品：buy: [{ id: '', count: '' }]
  // 当在购物车里点“去结算”时，就把购物车车中已经选中的商品放进去
  buy: getItem('buy')

};

// 定义 Vuex 模块的 getters，用于计算属性
const getters = {
  // getGoodsCount 方法计算每个商品的数量并存储在 goods 对象中
  getGoodsCount (state) {
    var goods = {};
    state.car.forEach(item => {
      goods[item.id] = item.count;
    });
    return goods;
  },
  // getGoodsSelected 方法存储每个商品的选中状态
  getGoodsSelected (state) {
    var goods = {};
    state.car.forEach(item => {
      goods[item.id] = item.selected;
    });
    return goods;
  },
  // getSelectedCount 方法计算选中商品的总数量
  getSelectedCount (state) {
    var count = 0;
    state.car.forEach(item => {
      if (item.selected) {
        count += item.count;
      }
    });
    return count;
  },
  // getSelectedGoods 方法存储选中的商品信息
  getSelectedGoods (state) {
    var goods = {};
    state.car.forEach(item => {
      if (item.selected) {
        goods[item.id] = item;
      }
    });
    return goods;
  },
  // getBuy 方法存储要购买的商品信息
  getBuy (state) {
    var goods = {};
    state.buy.forEach(item => {
      goods[item.id] = item;
    });
    return goods;
  },
  getSelectedCount (state) {
    var count = 0
    state.car.forEach(item => {
      if (item.selected) {
        count += item.count
      }
    })
    return count
  },
  getSelectedGoods (state) {
    var goods = {}
    state.car.forEach(item => {
      if (item.selected) {
        goods[item.id] = item
      }
    })
    return goods
  },
  getBuy (state) {
    var goods = {}
    state.buy.forEach(item => {
      goods[item.id] = item
    })
    return goods
  }



};

// 定义 Vuex 模块的 actions，目前为空
const actions = {};

// 定义 Vuex 模块的 mutations，包含 addCar 方法
const mutations = {
  addCar (state, goodsinfo) {
    var flag = false;
    // 遍历 car 数组，检查是否已存在相同 id 的商品
    state.car.some(item => {
      if (item.id == goodsinfo.id) {
        item.count += parseInt(goodsinfo.count);
        flag = true;
        return true;
      }
    });
    // 如果不存在相同 id 的商品，将新商品添加到 car 数组中
    if (!flag) {
      state.car.push(goodsinfo);
    }
    // 保存更新后的购物车信息到本地存储
    setItem('car', state.car);
  },
  updateGoodsInfo (state, goodsinfo) {
    state.car.some(item => {
      if (item.id == goodsinfo.id) {
        item.count = parseInt(goodsinfo.count)
        return true
      }
    })
    setItem('car', state.car)
  },
  updateGoodsSelected (state, info) {
    state.car.some(item => {
      if (item.id == info.id) {
        item.selected = info.selected
      }
    })
    setItem('car', state.car)
  },
  removeCar (state, id) {
    state.car.some((item, i) => {
      if (item.id == id) {
        state.car.splice(i, 1)
        return true
      }
    })
    setItem('car', state.car)
  },

  setBuy (state,goods) {
    state.buy = []
    if(goods){
      state.buy.push(goods)
    }else{
      
    state.car.some(item => {
      if (item.selected) {
        state.buy.push(item)
      }
    })
  }
    setItem('buy', state.buy)
  }


};


export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
};